package com.financing.platform.web.api;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FpFundInfoDTO;
import com.financing.platform.dto.ProjectDTO;
import com.financing.platform.dto.RecommentProductDTO;
import com.financing.platform.service.IFpFundInfoService;
import com.financing.platform.service.IProjectService;
import com.financing.platform.service.IRecommentProductService;
import com.financing.platform.util.DTOdateSet;

/**
 * 
 * @ClassName: recommentProductAction
 *
 */
@Controller("recommentProductAction")
@RequestMapping("/api/recommentProduct")
@Scope("prototype")
public class RecommentProductAction extends BaseAction{
	
	@Autowired
	private IRecommentProductService recommentProductService;
	
	@Autowired
	private IProjectService projectService;
	@Autowired
	private IFpFundInfoService fpFundInfoService;
	
	/**
	 * 
	 * @Title: addrecommentProduct
	 * @Description:推荐产品
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/addRecommentProduct.shtml")
	@ResponseBody
	public ResultMap addRecommentProduct(HttpServletRequest request,
			HttpServletResponse response,RecommentProductDTO recommentProduct) {
		this.log.info("begin addRecommentProduct info!");
		try{
			//默认给   热销
			if(StringUtils.isBlank(recommentProduct.getRecommentType())){
				recommentProduct.setRecommentType("1");
			}
			if(recommentProduct==null
					||StringUtils.isBlank(recommentProduct.getProductCode())||StringUtils.isBlank(recommentProduct.getProductType())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			boolean flag=false;
			//判断是否有该产品
			try {
				if(recommentProduct.getProductType().equals("01")){
					FpFundInfoDTO dto=new FpFundInfoDTO();
					dto.setId(Integer.parseInt(recommentProduct.getProductCode()));
					int fpFundInfo = fpFundInfoService.countFpFundInfo(dto);
					flag=fpFundInfo>0;
				}else{
					ProjectDTO projectDto=new ProjectDTO();
					projectDto.setpCode(recommentProduct.getProductCode());
					projectDto.setpType(recommentProduct.getProductType());
					List<ProjectDTO> queryAllProject = projectService.queryAllProject(projectDto);
					flag=queryAllProject!=null&&queryAllProject.size()>0;
				}
			} catch (Exception e1) {
				log.error("query project error",e1);
			}
			
			if(!flag){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PRODUCT_NOTEXIST);
				return resultMap;
			}
			
			//判断是否已存在
			List<Map<String, String>> queryRecommentProduct = recommentProductService.queryRecommentProduct(recommentProduct);
			if(queryRecommentProduct!=null&&queryRecommentProduct.size()>0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			
			// 初始化数据赋值
			DTOdateSet.setCreateInfo(recommentProduct, request.getSession());
			DTOdateSet.setUpdateInfo(recommentProduct, request.getSession());
			recommentProduct.setDeleteTag("1");
			try {
				recommentProductService.addRecommentProduct(recommentProduct);
			} catch (Exception e) {
				this.log.info("add error",e);
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
		}catch(Exception e){
			this.log.error("error addRecommentProduct",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end addRecommentProduct info access !");
		return resultMap;
	}
	
	/**
	 * 
	 * @Title: queryRecommentProduct
	 * @Description:查询推荐产品
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/queryRecommentProduct.shtml")
	@ResponseBody
	public ResultMap queryRecommentProduct(HttpServletRequest request,
			HttpServletResponse response,RecommentProductDTO recommentProduct,Pager page) {
		this.log.info("begin queryRecommentProduct info!");
		try{
			//默认查询 热销产品
//			if(StringUtils.isBlank(recommentProduct.getRecommentType())){
//				recommentProduct.setRecommentType("1");
//			}
			int count=recommentProductService.queryRecommentProductCount(recommentProduct);
			page.setTotalCount(count);
			recommentProduct.setBegin(page.getStartIndex());
			recommentProduct.setEnd(page.getPageSize());
			 List<Map<String, String>> list =new ArrayList<Map<String,String>>();
			if(count>0){
				list=recommentProductService.queryRecommentProduct(recommentProduct);
			}
			 resultMap.setAttribute("list",list);
			 resultMap.setAttribute("page",page);
			 resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			this.log.error("error queryRecommentProduct",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end queryRecommentProduct info access !");
		return resultMap;
	}
	
	/**
	 * 
	 * @Title: replayAdd
	 * @Description:是否已经添加
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/replayAddRecomment.shtml")
	@ResponseBody
	public ResultMap replayAddRecomment(HttpServletRequest request,
			HttpServletResponse response,RecommentProductDTO recommentProduct) {
		this.log.info("begin replayAdd info!");
		try{
			
			//默认查询 热销产品
//			if(StringUtils.isBlank(recommentProduct.getRecommentType())){
//				recommentProduct.setRecommentType("1");
//			}
			
			if(recommentProduct==null
					||StringUtils.isBlank(recommentProduct.getProductCode())||StringUtils.isBlank(recommentProduct.getProductType())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			List<Map<String, String>> queryRecommentProduct = recommentProductService.queryRecommentProduct(recommentProduct);
			if(queryRecommentProduct!=null&&queryRecommentProduct.size()>0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			 resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			this.log.error("error replayAdd",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end replayAdd info access !");
		return resultMap;
	}
	
	/**
	 * 
	 * @Title: delRecommentProduct
	 * @Description:绑定推荐产品
	 * @param @param request
	 * @param @param response
	 * @param @return   
	 * @return ModelAndView   
	 * @throws
	 */
	@RequestMapping("/delRecommentProduct.shtml")
	@ResponseBody
	public ResultMap delRecommentProduct(HttpServletRequest request,
			HttpServletResponse response,RecommentProductDTO recommentProduct) {
		
		this.log.info("begin delRecommentProduct info!");
		
		try{
			if(recommentProduct==null||StringUtils.isBlank(recommentProduct.getId())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			recommentProductService.deleteRecommentProduct(recommentProduct);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
		}catch(Exception e){
			this.log.error("error delRecommentProduct",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		this.log.info("end delRecommentProduct info access !");
		return resultMap;
	}
	
}
